package com.mbds.utils;

import java.util.List;
import java.util.Map;

import android.util.Log;

/** 
 * Get and analyse data from data base
 *
 */
public class TruncateTask extends Abs_Task {
	private static final int TRUNCATE_DELAY = 1000;//ms
	private static final float SIGMA = 7;
	
	/**
	 * Indicate if we have a notable difference between each data
	 * from data base, according to SIGMA
	 * @param values : values to analyze
	 * @return true if we have a notable , false otherwise
	 */
	private boolean notableDifferenceDetected(Map<String,List<Float>> values) {
		for (String key : values.keySet()) {
			List<Float> listValues = values.get(key);
			for(int i=1;i<listValues.size();i++) {
				if(listValues.get(i) - listValues.get(i-1) > SIGMA) {
					return true;
				}
			}
		}
		return false;
	}
	
	@Override
	public void run() {
		if(cancel) {
			return;
		}
		Map<String,List<Float>> accelerometersValues = db.getAndDeleteValues();
		if(notableDifferenceDetected(accelerometersValues)) {
			//TODO broacast on P2P network
			Log.i("DDDDDDDDDDDDDDDDDDDDD","DYFI");
		}
	}
	
	@Override
	public int getDelay() {
		return TRUNCATE_DELAY;
	}

}
